<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>使用nodejs + ts + koa搭建服务端脚手架 --- 1. 环境搭建 | 云游</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="favicon.ico">
    <script src="/live2dw/L2Dwidget.min.js"></script>
    <script src="/live2dw/L2Dwidget.0.min.js"></script>
    <script src="/live2dw/initLine2dw.js"></script>
    <link rel="stylesheet" href="/live2dw/initLine2dw.css">
    <meta name="description" content="仰天大笑出门去">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.746f5c46.css" as="style"><link rel="preload" href="/assets/js/app.8ca67ad1.js" as="script"><link rel="preload" href="/assets/js/3.dc5e7bc7.js" as="script"><link rel="preload" href="/assets/js/1.f19b5812.js" as="script"><link rel="preload" href="/assets/js/40.e4393725.js" as="script"><link rel="prefetch" href="/assets/js/10.063f4d84.js"><link rel="prefetch" href="/assets/js/11.7aaafe6d.js"><link rel="prefetch" href="/assets/js/12.1d3cbcb7.js"><link rel="prefetch" href="/assets/js/13.3eff3441.js"><link rel="prefetch" href="/assets/js/14.d011e66e.js"><link rel="prefetch" href="/assets/js/15.64da65a0.js"><link rel="prefetch" href="/assets/js/16.afe5f3d7.js"><link rel="prefetch" href="/assets/js/17.9645712f.js"><link rel="prefetch" href="/assets/js/18.8acac1b3.js"><link rel="prefetch" href="/assets/js/19.211aa7fa.js"><link rel="prefetch" href="/assets/js/20.33c40850.js"><link rel="prefetch" href="/assets/js/21.a5f61bc3.js"><link rel="prefetch" href="/assets/js/22.3f31af86.js"><link rel="prefetch" href="/assets/js/23.c1a2f360.js"><link rel="prefetch" href="/assets/js/24.f92207d5.js"><link rel="prefetch" href="/assets/js/25.3859fc8e.js"><link rel="prefetch" href="/assets/js/26.c3bb43ea.js"><link rel="prefetch" href="/assets/js/27.2d3ac28b.js"><link rel="prefetch" href="/assets/js/28.5eba4e00.js"><link rel="prefetch" href="/assets/js/29.591a72c1.js"><link rel="prefetch" href="/assets/js/30.4b4e3dae.js"><link rel="prefetch" href="/assets/js/31.0c3aac1f.js"><link rel="prefetch" href="/assets/js/32.927b230d.js"><link rel="prefetch" href="/assets/js/33.4ecffa3d.js"><link rel="prefetch" href="/assets/js/34.b9bc1b48.js"><link rel="prefetch" href="/assets/js/35.42f94296.js"><link rel="prefetch" href="/assets/js/36.95b6cc33.js"><link rel="prefetch" href="/assets/js/37.f3505924.js"><link rel="prefetch" href="/assets/js/38.19c4c627.js"><link rel="prefetch" href="/assets/js/39.592f8fae.js"><link rel="prefetch" href="/assets/js/4.47f43f55.js"><link rel="prefetch" href="/assets/js/41.66b5758c.js"><link rel="prefetch" href="/assets/js/42.b0eb455d.js"><link rel="prefetch" href="/assets/js/43.11fab467.js"><link rel="prefetch" href="/assets/js/44.cd6e5a73.js"><link rel="prefetch" href="/assets/js/45.b3e3b872.js"><link rel="prefetch" href="/assets/js/46.07a07f51.js"><link rel="prefetch" href="/assets/js/47.c2287444.js"><link rel="prefetch" href="/assets/js/48.66123573.js"><link rel="prefetch" href="/assets/js/49.bebbe116.js"><link rel="prefetch" href="/assets/js/5.e8c05b51.js"><link rel="prefetch" href="/assets/js/50.70436f90.js"><link rel="prefetch" href="/assets/js/51.18985c0f.js"><link rel="prefetch" href="/assets/js/52.ace084da.js"><link rel="prefetch" href="/assets/js/53.02616c91.js"><link rel="prefetch" href="/assets/js/54.6eb33990.js"><link rel="prefetch" href="/assets/js/55.733d5e7a.js"><link rel="prefetch" href="/assets/js/56.36ca4523.js"><link rel="prefetch" href="/assets/js/57.80493e39.js"><link rel="prefetch" href="/assets/js/58.34b95d6c.js"><link rel="prefetch" href="/assets/js/59.96be3833.js"><link rel="prefetch" href="/assets/js/6.027fb3c9.js"><link rel="prefetch" href="/assets/js/60.214c4180.js"><link rel="prefetch" href="/assets/js/61.ba0ebff0.js"><link rel="prefetch" href="/assets/js/62.09089206.js"><link rel="prefetch" href="/assets/js/63.a5c5aa3e.js"><link rel="prefetch" href="/assets/js/64.2d5fc9ca.js"><link rel="prefetch" href="/assets/js/65.c8171d3c.js"><link rel="prefetch" href="/assets/js/66.6d4bbf9e.js"><link rel="prefetch" href="/assets/js/67.ceeafab4.js"><link rel="prefetch" href="/assets/js/68.16d3218f.js"><link rel="prefetch" href="/assets/js/7.b5f855a8.js"><link rel="prefetch" href="/assets/js/8.5ac3177c.js"><link rel="prefetch" href="/assets/js/9.8e7f0e06.js">
    <link rel="stylesheet" href="/assets/css/0.styles.746f5c46.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>云游</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>仰天大笑出门去</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>fhtwl</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">云游</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/其它/" class="nav-link"><i class="undefined"></i>
  其它
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/fhtwl/fhtwl.github.io" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><!----> <h3 class="name" data-v-39576ba9>
    fhtwl
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>57</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>64</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/其它/" class="nav-link"><i class="undefined"></i>
  其它
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/fhtwl/fhtwl.github.io" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>使用nodejs + ts + koa搭建服务端脚手架 --- 1. 环境搭建</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>fhtwl</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-1aefc0b4><main class="page" style="padding-right:0;"><section><div class="page-title"><h1 class="title">使用nodejs + ts + koa搭建服务端脚手架 --- 1. 环境搭建</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>fhtwl</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>7/21/2021</span></i> <i class="iconfont reco-eye" data-v-f875f3fc><span id="/blog/nodejs/1.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-f875f3fc><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>js</span><span class="tag-item" data-v-f875f3fc>ts</span><span class="tag-item" data-v-f875f3fc>nodejs</span><span class="tag-item" data-v-f875f3fc>koa2</span><span class="tag-item" data-v-f875f3fc>cli</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="使用nodejs-ts-koa搭建服务端脚手架-1-环境搭建"><a href="#使用nodejs-ts-koa搭建服务端脚手架-1-环境搭建" class="header-anchor">#</a> 使用nodejs + ts + koa搭建服务端脚手架 --- 1. 环境搭建</h2> <h4 id="ts-环境搭建"><a href="#ts-环境搭建" class="header-anchor">#</a> ts 环境搭建</h4> <ol><li>全局安装ts</li></ol> <div class="language- extra-class"><pre class="language-text"><code>npm i typescript -g
</code></pre></div><ol start="2"><li>创建package.json：</li></ol> <div class="language-sh extra-class"><pre class="language-sh"><code><span class="token function">npm</span> init
</code></pre></div><ol start="3"><li>创建tsconfig.json</li></ol> <div class="language-sh extra-class"><pre class="language-sh"><code>tsc --init
</code></pre></div><p>tsconfig.json就是ts项目的配置文件。<br></p> <p>打开tsconfig.json，修改以下参数：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">{</span>
  <span class="token string-property property">&quot;compilerOptions&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token string-property property">&quot;target&quot;</span><span class="token operator">:</span> <span class="token string">&quot;ES2015&quot;</span><span class="token punctuation">,</span>                                <span class="token comment">/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */</span>
    <span class="token string-property property">&quot;module&quot;</span><span class="token operator">:</span> <span class="token string">&quot;commonjs&quot;</span><span class="token punctuation">,</span>                           <span class="token comment">/* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */</span>
    <span class="token string-property property">&quot;lib&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token string">&quot;es2015&quot;</span><span class="token punctuation">,</span>
      <span class="token string">&quot;ESNext&quot;</span><span class="token punctuation">,</span>
      <span class="token string">&quot;DOM&quot;</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>                                   <span class="token comment">/* Specify library files to be included in the compilation. */</span>
    <span class="token string-property property">&quot;declaration&quot;</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>                         <span class="token comment">/* Generates corresponding '.d.ts' file. */</span>
    <span class="token string-property property">&quot;sourceMap&quot;</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>                           <span class="token comment">/* Generates corresponding '.map' file. */</span>
    <span class="token string-property property">&quot;outDir&quot;</span><span class="token operator">:</span> <span class="token string">&quot;./dist/&quot;</span><span class="token punctuation">,</span>                           <span class="token comment">/* Redirect output structure to the directory. */</span>
    <span class="token string-property property">&quot;rootDir&quot;</span><span class="token operator">:</span> <span class="token string">&quot;./src/&quot;</span><span class="token punctuation">,</span>                             <span class="token comment">/* Specify the root directory of input files. Use to control the output directory structure with --outDir. */</span>
    <span class="token string-property property">&quot;strict&quot;</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>                                 <span class="token comment">/* Enable all strict type-checking options. */</span>
    <span class="token string-property property">&quot;moduleResolution&quot;</span><span class="token operator">:</span> <span class="token string">&quot;node&quot;</span><span class="token punctuation">,</span>                  <span class="token comment">/* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */</span>
    <span class="token string-property property">&quot;baseUrl&quot;</span><span class="token operator">:</span> <span class="token string">&quot;./src&quot;</span><span class="token punctuation">,</span>                             <span class="token comment">/* Base directory to resolve non-absolute module names. */</span>
    <span class="token string-property property">&quot;esModuleInterop&quot;</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>                        <span class="token comment">/* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */</span>
    <span class="token string-property property">&quot;experimentalDecorators&quot;</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>              <span class="token comment">/* Enables experimental support for ES7 decorators. */</span>
    <span class="token string-property property">&quot;skipLibCheck&quot;</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>                           <span class="token comment">/* Skip type checking of declaration files. */</span>
    <span class="token string-property property">&quot;forceConsistentCasingInFileNames&quot;</span><span class="token operator">:</span> <span class="token boolean">true</span>        <span class="token comment">/* Disallow inconsistently-cased references to the same file. */</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;include&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token string">&quot;src/**/*&quot;</span>
  <span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;files&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token string">&quot;./src/app.ts&quot;</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span>


</code></pre></div><p>我们需要关注的属性，主要是以下几个：</p> <ol><li><p>compilerOptions.target: 最后编译的代码的es版本</p></li> <li><p>compilerOptions.module: 指定生成哪个模块系统代码</p></li> <li><p>compilerOptions.lib: 编译过程中需要引入的库文件的列表</p></li> <li><p>compilerOptions.outDir: 输出目录</p></li> <li><p>compilerOptions.include： 指定一个匹配列表（属于自动指定该路径下的所有ts相关文件）</p></li> <li><p>compilerOptions.files: 指定哪些文件使用该配置（属于手动一个个指定文件）</p></li></ol> <p>其它的可以去查看官方文档 <br></p> <ol start="3"><li><p>创建开发目录</p> <p>在根目录下创建src目录，并新建一个app.ts，这个就是我们项目的入口文件</p></li></ol> <div class="language-typescript extra-class"><pre class="language-typescript"><code><span class="token comment">// app.ts</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span>
    
<span class="token punctuation">}</span>

</code></pre></div><p>然后编译项目，命令行输入：</p> <div class="language- extra-class"><pre class="language-text"><code>tsc
</code></pre></div><p>编译完成之后，就能在根目录下的dist目录下看到编译的js文件</p> <h4 id="nodejs、koa环境搭建"><a href="#nodejs、koa环境搭建" class="header-anchor">#</a> nodejs、koa环境搭建</h4> <ol><li>安装koa</li></ol> <p>命令行输入：</p> <div class="language- extra-class"><pre class="language-text"><code>npm i koa @types/koa --save
</code></pre></div><p>修改app.ts，引入koa</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token comment">// app.ts</span>
<span class="token comment">// 引入koa和http模块</span>
<span class="token keyword">import</span> Koa <span class="token keyword">from</span> <span class="token string">&quot;koa&quot;</span>
<span class="token keyword">import</span> http <span class="token keyword">from</span> <span class="token string">'http'</span>
<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Koa</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> server<span class="token operator">:</span> http<span class="token punctuation">.</span>Server <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">http</span><span class="token punctuation">.</span><span class="token function">Server</span><span class="token punctuation">(</span>app<span class="token punctuation">.</span><span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span>ctx<span class="token operator">:</span> Koa<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  ctx<span class="token punctuation">.</span>body <span class="token operator">=</span> <span class="token string">'Hello World'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 启动服务器，监听3001端口</span>
server<span class="token punctuation">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token number">3001</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'app started at port 3001...'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

</code></pre></div><ol start="2"><li>运行项目</li></ol> <p>运行ts的方法有两种，一种是编译为js后再运行：</p> <div class="language- extra-class"><pre class="language-text"><code>tsc
nodejs ./dist/app.js
</code></pre></div><p>第二种方式更加方便，使用<b>ts-node</b>直接运行ts文件</p> <div class="language- extra-class"><pre class="language-text"><code>npm i ts-node
ts-node ./src/app.ts
</code></pre></div><p>项目运行成功之后，我们就能在命令行中看到我们console的文字:</p> <blockquote><p>app started at port 3001...</p></blockquote> <p>打开浏览器，访问localhost:3001，也能在页面上看到我们返回给浏览器的文字：</p> <blockquote><p>Hello World</p></blockquote> <ol start="3"><li>监听文件变化并自动更新程序</li></ol> <p>目前我们已经启动了服务器，但是没当我们修改的代码，都需要程序启动项目，才能更新，这太麻烦。<br></p> <p>我们可以使用<b>nodemon</b>，来帮助我们实现监听文件变化并自动重新启动nodejs的应用程序：</p> <div class="language- extra-class"><pre class="language-text"><code>npm i nodemon

</code></pre></div><p>打开package，在scripts里，添加一行：</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token property">&quot;scripts&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;dev&quot;</span><span class="token operator">:</span> <span class="token string">&quot;nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./src/app.ts&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre></div><p>通过命令行运行：</p> <div class="language- extra-class"><pre class="language-text"><code>npm run dev
</code></pre></div><p>这时可以发现，当我们保存的文化，程序就会自动更新。</p> <h4 id="总结"><a href="#总结" class="header-anchor">#</a> 总结</h4> <p>在上述内容里，我们创建了一个nodejs、ts、koa的服务端项目，安装了核心的依赖包，通过代码运行起了服务器，并实现了监听文件变化并自动刷新。<br></p></div></section> <footer class="page-edit"><!----> <!----></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:0;" data-v-cb1513f6></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/assets/js/app.8ca67ad1.js" defer></script><script src="/assets/js/3.dc5e7bc7.js" defer></script><script src="/assets/js/1.f19b5812.js" defer></script><script src="/assets/js/40.e4393725.js" defer></script>
  </body>
</html>
